#ifndef serialio
#define serialio

#include "platform.h"

typedef struct
{
#if defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN
    HANDLE hCom;
#endif // defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN

#if defined COMPILE_FOR_LINUX || defined COMPILE_FOR_LPC21
    int fdCom;
#endif // defined COMPILE_FOR_LINUX || defined COMPILE_FOR_LPC21

#if defined COMPILE_FOR_LINUX
    struct termios oldtio, newtio;
#endif // defined COMPILE_FOR_LINUX

    unsigned serial_timeout_count;   // Local used to track timeouts on serial port read.
    unsigned char HalfDuplex;        // Only used for LPC Programming

    unsigned char ControlLines;
    unsigned char ControlLinesSwapped;
    unsigned char ControlLinesInverted;
    unsigned char LogFile;
} termioType;

//Open/Close
unsigned char OOpenSerialPort(termioType *pio,  const char *port, const char *baud);
void OCloseSerialPort(termioType *pio);

//Clear buffers
void OClearSerialPortBuffers(const termioType *pio);

//Receive bytes
int OReceiveComPortBlockComplete(const termioType *pio,
                                void *block, size_t size, unsigned timeout);

void OReceiveComPort(const termioType *pio,
                    const char *Ans, unsigned int MaxSize,
                    unsigned int *RealSize, unsigned int WantedNr0x0A,
                    unsigned timeOutMilliseconds);

//Send bytes
void OSendComPort(const termioType *pio, const char *s);
void OSendComPortBlock(const termioType *pio, const void *s, size_t n);

//Control RTS, DTR, ...
void OControlModemLines(const termioType *pio, unsigned char DTR, unsigned char RTS);

#endif